import { defHttp } from '@/utils/http/axios';
import {ContentTypeEnum, RequestEnum} from '@/enums/httpEnum';
import { ServicePrefixEnum } from '@/enums/commonEnum';
import type { AxiosRequestConfig } from 'axios';

const MODULAR = '/system/oauth2';

export const Api = {
  GetAuthorize: {
    url: `${ServicePrefixEnum.BASE}/${MODULAR}/authorize`,
    method: RequestEnum.GET,
  } as AxiosRequestConfig,
  Authorize: {
    url: `${ServicePrefixEnum.BASE}/${MODULAR}/authorize`,
    method: RequestEnum.POST,
  } as AxiosRequestConfig
}

// 获得授权信息
export const getAuthorize = (clientId: string) => {
  let params = {clientId:clientId}
  return defHttp.request({ ...Api.GetAuthorize, params })
}

// 发起授权
export const authorize = (
    responseType: string,
    clientId: string,
    redirectUri: string,
    state: string,
    autoApprove: boolean,
    checkedScopes: string[],
    uncheckedScopes: string[]
) => {
  // 构建 scopes
  const scopes = {}
  for (const scope of checkedScopes) {
    scopes[scope] = true
  }
  for (const scope of uncheckedScopes) {
    scopes[scope] = false
  }
  // 发起请求
  let params = {
    response_type: responseType,
    client_id: clientId,
    redirect_uri: redirectUri,
    state: state,
    auto_approve: autoApprove,
    scope: JSON.stringify(scopes)
  }
  let headers = {
    'Content-type': ContentTypeEnum.FORM_URLENCODED,
  }
  return defHttp.request({ ...Api.Authorize, params:params,headers:headers })
}


// 获得授权信息
// export const getAuthorize = (clientId: string) => {
//   return request.get({ url: '/system/oauth2/authorize?clientId=' + clientId })
// }

// 发起授权
// export const authorize = (
//   responseType: string,
//   clientId: string,
//   redirectUri: string,
//   state: string,
//   autoApprove: boolean,
//   checkedScopes: string[],
//   uncheckedScopes: string[]
// ) => {
//   // 构建 scopes
//   const scopes = {}
//   for (const scope of checkedScopes) {
//     scopes[scope] = true
//   }
//   for (const scope of uncheckedScopes) {
//     scopes[scope] = false
//   }
//   // 发起请求
//   return request.post({
//     url: '/system/oauth2/authorize',
//     headers: {
//       'Content-Type': 'application/x-www-form-urlencoded'
//     },
//     params: {
//       response_type: responseType,
//       client_id: clientId,
//       redirect_uri: redirectUri,
//       state: state,
//       auto_approve: autoApprove,
//       scope: JSON.stringify(scopes)
//     }
//   })
// }
